package com.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.Bean.wdhtprojectModel;
import com.Service.wdhtbuildService;
import com.Service.wdhtprojectService;
import com.Service.wdhtroommodelService;

@Controller
@RequestMapping("/hdproject")
public class wdprojectAction {
	@Autowired
	private wdhtprojectService wdhtprojectService;
	@Autowired
	private wdhtbuildService servicebuild;
	@Autowired
	private wdhtroommodelService serviceroom;
	
	@RequestMapping("/listhdproject.do")
	public String list(Model model){
	  List<wdhtprojectModel> selectall = wdhtprojectService.selectall();
	  model.addAttribute("listt",selectall);
	  return "Wdproject";
	}
	@RequestMapping("/goaddhdproject.do")
	public String goadd(Model model){
		  return "Addhdproject";
		}
	@RequestMapping("/addhdproject.do")
	public String add(wdhtprojectModel bean){
		wdhtprojectService.insertSelective(bean);		
		  return "redirect:listhdproject.do";
		}
	
	@RequestMapping("/deletehdproject.do")
		public String delete(Model model,wdhtprojectModel a){
		try {
			a.setProjectName(new String(a.getProjectName().getBytes("ISO-8859-1"),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		    System.out.println(a.getProjectName());
			wdhtprojectService.deleteByPrimaryKey(a.getProjectName());
			servicebuild.deleteByPrimaryKey(a.getProjectName());
			serviceroom.deleteByPrimaryName(a.getProjectName());
			  return "redirect:listhdproject.do";
			}
    @InitBinder  
	 public void initBinder(WebDataBinder binder) {  
	     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	     dateFormat.setLenient(false);  
	     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	 }
    @RequestMapping("/goupdatehdproject.do")
	public String goupdate(Model model,wdhtprojectModel a){
    	try {
			a.setProjectName(new String(a.getProjectName().getBytes("ISO-8859-1"),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	wdhtprojectModel selectByPrimaryKey = wdhtprojectService.selectByPrimaryKey(a.getProjectName()); 
		 model.addAttribute("bean",selectByPrimaryKey);
		  return "Updatehdproject";
		}
    @RequestMapping("/updatehdproject.do")
    public String updatehdproject(wdhtprojectModel bean){
    	System.out.println(bean.getProjectId());
    	wdhtprojectService.updateByPrimaryKeySelective(bean);
    	return "redirect:listhdproject.do";   
    }

}